/*
	Copyright (C) 2009 jungil Han <jungil.Han@gmail.com>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mkHttp.h"
#include "../../Button/mkHotkeyButton.h"

#include <QUrl>
#include <QByteArray>
#include <QBuffer>
//#include <QMessageBox>
//#include <QFile>

mkHttp::mkHttp( IN QObject *a_pParent ) : QHttp( a_pParent )
{
	m_pUrl = NULL;
	m_pHttpData = NULL;

	m_nHttpId = -1;
	m_bIsCompleted = false;

	connect( this, SIGNAL( responseHeaderReceived( const QHttpResponseHeader &)), this, SLOT( ReadResponseHeader( const QHttpResponseHeader &)));
	connect( this, SIGNAL( requestFinished( int, bool )), this, SLOT( HttpRequestFinished( int, bool )));
}

mkHttp::~mkHttp()
{
	MK_DELETE( m_pUrl );
	MK_DELETE( m_pHttpData );
}

void mkHttp::SetUrl( IN QString &a_refUrl )
{
	if ( !a_refUrl.contains( "http://", Qt::CaseInsensitive ))
		a_refUrl.insert( 0, "http://");

	MK_DELETE( m_pUrl );
	MK_DELETE( m_pHttpData );

	m_pUrl = new QUrl;

	m_pUrl->setUrl( a_refUrl );
}

int mkHttp::Connect()
{
	if ( !m_pUrl || m_nHttpId >= 0 )
		return -1;

	setHost( m_pUrl->host(), QHttp::ConnectionModeHttp, 80 );

	MK_DELETE( m_pHttpData );
	m_pHttpData = new QBuffer( this );

	m_nHttpId = get( "/favicon.ico", m_pHttpData );

	return m_nHttpId;
}

QString mkHttp::GetUrl()
{
	return m_pUrl->toString();
}

QString mkHttp::GetHost()
{
	return m_pUrl->host();
}

void mkHttp::ReadResponseHeader( const QHttpResponseHeader &a_refResponseHeader )
{
    switch ( a_refResponseHeader.statusCode() ) {
    case 200:	// Ok
    	break;
    case 301:	// Moved Permanently
    case 303:	// See Other
    case 307:	// Temporary Redirect
    case 302:	// Found
    default:
    	//QMessageBox::information( (QWidget*)parent(), "Http Response Header", tr("Download failed: %1.").arg( a_refResponseHeader.reasonPhrase() ));
        abort();
    }
}

void mkHttp::HttpRequestFinished( int a_nRequestId, bool a_bError )
{
	if ( a_nRequestId != m_nHttpId )
		return ;

	if ( a_bError ) {
		//QMessageBox::information( (QWidget*)parent(), "Http Request Finished", tr("Download failed: %1.").arg( errorString() ));
	} else {
		//QMessageBox::information( (QWidget*)parent(), "Http Request Finished", "Loading Completed!!");
	}

	// For Debug
#if 0
	//QPixmap favicon;
	m_pHttpData->open(QIODevice::ReadOnly);
	//favicon.loadFromData( m_pBuffer->readAll() );
	//m_pBuffer->close();

	QFile httpLogFile( "FAVICON.ico" );
	httpLogFile.open( QIODevice::WriteOnly );
	httpLogFile.write( m_pHttpData->readAll() );
	httpLogFile.close();

	m_pHttpData->close();
	//static_cast<mkHotkeyButton *>(parent())->SetFavicon( favicon );
#endif

	m_pHttpData->open( QIODevice::ReadOnly );
	QByteArray byteArray( m_pHttpData->readAll() );
	OnFinished( byteArray );
	m_pHttpData->close();

	m_nHttpId = -1;
	MK_DELETE( m_pHttpData );
	MK_DELETE( m_pUrl );
}
